define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');

	var previewAndPreAudit = view.widget.extend({
		init: function( config ) {
			config = pubjs.conf( config, {
				"preAuditUrl": 'creativeAudit/preAudit',
				"auditUrl": 'creativeAudit/audit',
				"getAuditResultUrl":'creativeAudit/getAuditResult',
				"layout": {
					"module": '@base/dialog.base',
					"mask": 1,
					"title": LANG('创意批量预审核'),
					"showFoot": true, // 显示底层的取消和完成按钮
					"width": 500,
					"close": null,
					"silence": false // 点击按钮发消息
					,"buttonConfig":{
						// 确定按钮样式
						"ok": {
							"value":LANG("确定")
						}
					}
				},
				'view_model':{
					_creatives:[{Name:'',_id:0}]
				}
			});
			this.Super( 'init', arguments );
			//缓存id
			this.$id = 0;
		},
		afterBuild:function(){
			var self = this;
			var el = self.getDOM();
			var html = [
				'<div class="P-dspUserCharge pl20">',
					'<div class="mb20 uk-form">',
						'<label class="M-formLabel mr10" style="vertical-align: top">' + LANG('创意名称：') + '</label>',
						'<div class="mr10 M-formInput" style="max-width: 600px;margin-left: 60px;">' +
							'<p ms-repeat-item="_creatives"><b ms-text="item.Name" /></p>' +
						'</div>',
					'</div>',
					'<div class="uk-form exchange" >',
						'<label class="M-formLabel mr10" style="vertical-align: top">' + LANG('渠道：') + '</label>',
						'<div style="display: inline-block" pub-name="exchangeIds" pub-mod="modules/checkBoxGroup.base"/>',
					'</div>',

					'<div class="mb20 uk-form preAudit">',
						'<label class="M-formLabel mr10">' + LANG('审核状态：') + '</label>',
						'<span pub-name="preStatus" pub-mod="@base/common/input.radioGroup"/>',
					'</div>',
					'<div class="mb20 uk-form preAudit">',
						'<label class="M-formLabel mr10">' + LANG('审核理由：') + '</label>',
						'<span class="mr10 M-formInput" pub-name="preInfo" pub-mod="@base/common/input.text"/>',
					'</div>',
				'</div>'
			].join('');
			$(html).appendTo( el );
			self.buildFromTemplate({
				'preStatus': {
					'data':[
						{text:LANG('通过'),value:'1'},
						{text:LANG('拒绝'),value:'-1'}
					]
				},
				'preInfo':{
					width:400
				}
			});
			this.setMode('preAudit');
		},
		setTitle: function(title){
			var layout = this.getLayout();
			layout.setTitle(title);
			return this;
		},
		setMode:function(modeName){
			switch (modeName|| 'preAudit'){
				case 'preAudit':
					this.find('.preAudit').show();
					this.$mode = 'preAudit';
					this.setTitle("创意批量预审核");
					break;
				case 'getAuditResult':
					this.$mode = 'getAuditResult';
					this.find('.preAudit').hide();
					this.setTitle("获取创意审核结果");
					break;
				case 'audit':
					this.$mode = 'audit';
					this.find('.preAudit').hide();
					this.setTitle("重新送审");
					break;
			}
			return this;
		},
		setData: function( data ){
			var self = this;

			var vm = this.vm.$;
			vm._creatives = data.creatives || [];

			this.$ids = data.ids;
			var exchangeMap = [],creative,audit, exchanges = [];
			if(util.isArray(data.creatives)){
				for(var i = 0; i < data.creatives.length; i++){
					creative = data.creatives[i];
					if (creative && creative.AuditList && creative.AuditList.length > 0){
						for (var j = 0;j < creative.AuditList.length; j++){
							audit = creative.AuditList[j];
							if(audit.ExchangeName && audit.ExchangeId){
								exchangeMap[audit.ExchangeId] = {id:audit.ExchangeId, name: audit.ExchangeName};
							}
						}
					}
				}

				for(var i in exchangeMap){
					exchanges.push(exchangeMap[i]);
				}
			}

			if(exchanges.length > 0){
				self.find('.exchange').show();
				self.$.exchangeIds.setData(exchanges);
				self.$.exchangeIds.show();
			}else {
				self.find('.exchange').hide();
				self.$.exchangeIds.setData([]);
				self.$.exchangeIds.hide();
			}

			if(this.$mode == "preAudit"){
				self.$.preStatus.setValue(data.status ? data.status : null);
				self.$.preInfo.setValue(data.preInfo ? data.preInfo : '');
			}

			return this;
		},
		getData: function(){
			var ids = [];
			if('Audit' == this.getConfig('idType')){
				var creative;
				for(var i = 0; i < this.vm.$._creatives.length; i++){
					creative = this.vm.$._creatives[i];
					if(creative.AuditId){
						ids.push(creative.AuditId);
					}
				}
			} else {
				ids = this.$ids;
			}
			return {
				ids: ids,
				idType:this.getConfig('idType'),
				preStatus: +this.$.preStatus.getValue(),
				preInfo: this.$.preInfo.getValue(),
				exchangeIds:this.$.exchangeIds.getValue()
			}
		},
		reset: function(){
			var vm = this.vm.$;
			var mods = this.$;

			vm._creatives = [];

			mods.preInfo.setValue('');
			mods.preStatus.reset();
			return this;
		},
		save: function(id){
			var data = this.getData();
			pubjs.loading.show();
			var c = this.getConfig();
			switch (this.$mode|| 'preAudit'){
				case 'preAudit':
					pubjs.mc.send(c.preAuditUrl, data, this.onSave.bind(this));
					break;
				case 'getAuditResult':
					pubjs.mc.send(c.getAuditResultUrl, data, this.onSave.bind(this));
					break;
				case 'audit':
					pubjs.mc.send(c.auditUrl, data, this.onSave.bind(this));
					break;
			}
			return this;
		},
		onSave: function(err, data){
			pubjs.loading.hide();
			if(err){
				pubjs.alert(err.message);
				return false;
			} else if(!data.success){
				pubjs.alert(data.message|| LANG("未知错误"));
				return false;
			}
			pubjs.alert(LANG(data.message || "操作成功"));
			this.reset();
			this.fire('auditSuccess');
			this.hide();
		},
		//响应点击完成事件
		onDialogOk: function(ev){
			this.save(this.$id);
		},
		validate: function(data){
			if(this.$mode == 'preAudit' && !data.preStatus){
				pubjs.alert(LANG('请选择审核状态'));
				return false;
			}
			return true;
		}
	});
	exports.base = previewAndPreAudit;
});